package cn.dankal.lequ.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.ui.fragment.bangdan_fragment.WangyouFragment;

public class BangdanActivity extends AppCompatActivity {
    public final static int FRAGMENT_WANGYOU = 0;
    public final static int FRAGMENT_DUJIA = 1;
    @Bind(R.id.tv_wangyou)
    TextView tv_wangyou;
    @Bind(R.id.tv_dujia)
    TextView tv_dujia;
    private WangyouFragment dujiaFragment;
    private WangyouFragment wangyouFragment;
    private int currentFragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bangdan);
        ButterKnife.bind(this);
        setDefaultFragment();
    }

    private void setDefaultFragment() {
        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        wangyouFragment = new WangyouFragment();
        ft.replace(R.id.autoll, wangyouFragment);
        ft.commit();
    }

    @OnClick({R.id.tv_wangyou, R.id.tv_dujia})
    public void onTabSwitch(View view) {
        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.tv_wangyou:
                tv_wangyou.setTextColor(this.getResources().getColor(R.color.colorPrimary));
                tv_dujia.setTextColor(Color.BLACK);
                if (currentFragment == FRAGMENT_WANGYOU)
                    return;
                ft.hide(dujiaFragment);
                ft.show(wangyouFragment);
                tv_dujia.setBackgroundColor(getResources().getColor(R.color.white));
                tv_wangyou.setBackgroundColor(getResources().getColor(R.color.viewpagerbgcolor));
                currentFragment = FRAGMENT_WANGYOU;
                break;
            case R.id.tv_dujia:
                if (currentFragment == FRAGMENT_DUJIA)
                    return;
                if (dujiaFragment == null) {
                    dujiaFragment = new WangyouFragment();
                    ft.add(R.id.autoll, dujiaFragment);
                }
                ft.hide(wangyouFragment);
                ft.show(dujiaFragment);
                tv_dujia.setBackgroundColor(getResources().getColor(R.color.viewpagerbgcolor));
                tv_dujia.setTextColor(this.getResources().getColor(R.color.colorPrimary));
                tv_wangyou.setTextColor(Color.BLACK);

                tv_wangyou.setBackgroundColor(getResources().getColor(R.color.white));
                currentFragment = FRAGMENT_DUJIA;
                break;
        }
        ft.commit();
    }
}
